const express = require('express');
const router = express.Router();
const db = require('../config/db');
const bcrypt = require('bcryptjs');

// 获取用户信息
router.get('/profile', async (req, res) => {
  try {
    const [user] = await db.promise().query(
      'SELECT id, username, email, avatar_url, created_at FROM users WHERE id = ?',
      [req.user.id]
    );

    if (user.length === 0) {
      return res.status(404).json({
        success: false,
        error: '用户不存在',
        code: 404
      });
    }

    res.json({
      success: true,
      data: user[0]
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message,
      code: 500
    });
  }
});

// 更新用户信息
router.put('/profile', async (req, res) => {
  try {
    const { username, email } = req.body;
    const user_id = req.user.id;

    await db.promise().query(
      'UPDATE users SET username = ?, email = ? WHERE id = ?',
      [username, email, user_id]
    );

    res.json({
      success: true,
      message: '用户信息更新成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message,
      code: 500
    });
  }
});

module.exports = router; 